package common {
	import flash.display.Shape;
	
	import interfaces.IDestroyable;
	
	import spawners.GenericSpawner;
	
	public class Tank implements IDestroyable {
		private static var _instance:Tank
		
		private var _crudSpawner:GenericSpawner;
		private var _crudGenChance:Number;
		private var _currentSpeed:Number;
		private var _currentDirection:Number;
		private var _waterColor:uint;
		private var _water:Shape;
		private var _width:int;
		private var _height:int;
		private var _mutationChance:Number = 0.0005;  //TODO: this should be pulled from the control bar
		
		private var _displayStats:Boolean;
		
		private var _destroyed:Boolean;
		
		public static function get instance():Tank {
			if (!_instance)
				_instance = new Tank();
				
			return _instance;
		}
		
		public function Tank() {
			_water = new Shape();
			_destroyed = false;
		}
		
		private function drawWater():void {
			_water.graphics.clear();
			_water.graphics.beginFill(_waterColor);
			_water.graphics.drawRect(0, 0, _width, _height);
			_water.graphics.endFill();
		}
		
		public function set crudSpawner(crudSpawnerIn:GenericSpawner):void {
			_crudSpawner = crudSpawnerIn;
		}
		
		public function get crudGenChance():Number {
			return _crudGenChance;
		}
		
		public function set crudGenChance(crudGenChanceIn:Number):void {
			_crudGenChance = crudGenChanceIn;
			
			if (_crudSpawner)
				_crudSpawner.spawnChance = _crudGenChance;
		}
		
		public function get currentSpeed():Number {
			return _currentSpeed;
		}
		
		public function set currentSpeed(currentSpeedIn:Number):void {
			_currentSpeed = currentSpeedIn;
		}

		public function get currentDirection():Number {
			return _currentDirection;
		}
		
		public function set currentDirection(currentDirectionIn:Number):void {
			_currentDirection = currentDirectionIn;
		}

		public function get width():Number {
			return _width;
		}
		
		public function set width(widthIn:Number):void {
			_width = widthIn;
			drawWater();
		}

		public function get height():Number {
			return _height;
		}
		
		public function set height(heightIn:Number):void {
			_height = heightIn;
			drawWater();
		}
		
		public function get water():Shape {
			return _water;
		}
		
		public function set waterColor(waterColorIn:uint):void {
			_waterColor = waterColorIn;
			drawWater();
		}
		
		public function get displayStats():Boolean {
			return _displayStats;
		}
		
		public function set displayStats(displayStatsIn:Boolean):void {
			_displayStats = displayStatsIn;
		}
		
		public function get mutationChance():Number {
			return _mutationChance;
		}
		
		public function get destroyed():Boolean {
			return _destroyed;
		}
		
		public function destroy():void {
			if (destroyed)
				return;
			_destroyed = true;
			
			_instance = null;
			_crudSpawner.destroy();
		}
	}
}
